package junit;

import java.util.List;
import java.util.*;

/**
 * @Author ITcz
 * @Data 2021-05-31 - 15:24
 */

public class kuaishou {
    public boolean FindKthToTail(String s, ArrayList<String> wordDict) {
        ArrayList<String> temp = new ArrayList<String>();
        //切割匹配
        for (int i = 0; i < wordDict.size(); i++) {
            //如果有相同的，则匹配成功
            if (s.equals(wordDict.get(i))) {
                return true;
            }
            if (s.split(wordDict.get(i), 1).length > 1) {
                temp.add(s.split(wordDict.get(i))[1]);
            }
        }
        //没有匹配上的单词
        if (temp == null) {
            return false;
        }
        while (temp != null) {
            ArrayList<String> list = new ArrayList<String>();
            for (int i = 0; i < wordDict.size(); i++) {
                //依次从取出temp中取出 与字典比较
                for (int i1 = 0; i1 < temp.size(); i1++) {
                    //如果有相同的，则匹配成功
                    if (temp.get(i1).equals(wordDict.get(i))) {
                        return true;
                    }
                    //没有匹配玩完，继续匹配
                    if (temp.get(i1).split(wordDict.get(i), 1).length > 1) {
                        //如果有匹配则添加至临时list
                        list.add(temp.get(i1).split(wordDict.get(i), 1)[1]);
                    }
                }
            }
            temp = list;
        }
        return false;
    }
}
